package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.service.wx.WxSearchService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/search")
public class WxSearchController {

    @Autowired
    WxSearchService wxSearchService;
    @Autowired
    SecurityManager securityManager;

    /**
     * 搜索
     * @return
     */
    @GetMapping("index")
    public Object index(){
        // 获取用户id
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        Map<String,Object> result = wxSearchService.searchIndex(userId);

        return ResponseUtil.ok(result);
    }

    /**
     * 联想的结果
     * @param keyword
     * @return
     */
    @GetMapping("helper")
    public  Object helper(String keyword){
        List<String> associativeKeyword = wxSearchService.getAssociativeKeyword(keyword);

        return ResponseUtil.ok(associativeKeyword);
    }

    /**
     * 清空搜索记录
     * @return
     */
    @PostMapping("clearhistory")
    public Object clearhistory(){
        // 获取用户id
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        wxSearchService.clearHistory(userId);
        return ResponseUtil.ok();
    }
}
